#!/usr/local/bin/python3.6
# coding:utf-8
# @Time    : 2019/11/14 18:22
# @Author  : ZhangBing
# @Email   : 18829272841@163.com
# @File    : modules.py
# @Software: PyCharm
from exts import db
from datetime import datetime


class User(db.Model):
    __tabelname__ = 'user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(50), nullable=False)
    username = db.Column(db.String(50), nullable=False)
    password = db.Column(db.String(200), nullable=False)

    def __init__(self, username, email, password):
        self.username = username
        self.password = password
        self.email = email

    def __repr__(self):
        return '<User  id:{}  email: {}  username:{}  password:{}>'.format(self.id, self.email, self.username,
                                                                           self.password)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    title = db.Column(db.String(128), nullable=False)
    content = db.Column(db.TEXT, nullable=False)
    # 此处如果定义为now()，则其默认是在初始化时直接进行时间处理而不是在每次插入时进行时间处理
    create_time = db.Column(db.DateTime, default=datetime.now)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    author = db.relationship('User', backref=db.backref('users'))  # 此处用于定义生成时调用的posts 用于从用户处调用,此处是一对多的关系

    def __init__(self, title, content, author_id):
        self.title = title
        self.content = content
        self.author_id = author_id

    def __repr__(self):
        return '<Post id:{}  title:{} content:{}  create_time:{} author_id:{}>'.format(self.id, self.title,
                                                                                       self.content, self.create_time,
                                                                                       self.author_id)

    __str__ = __repr__


if __name__ == "__main__":
    pass
